package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/ydd_advpos")
public class AdvposController {


    @Autowired
    private AdvimgService advimgService;

    @Autowired
    private AdvposService advposService;



    //分页查询
    @GetMapping("/list")
    public ResultData list(String advposdesc, String advposname, Long page, Long limit){


        Page<Advpos> pageinfo = new Page(page,limit);

        Page<AdvposDto> advposDtoPage = new Page<>();

        LambdaQueryWrapper<Advpos> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(advposname),Advpos::getAdvposname,advposname);


        //广告位数据分页查询
        Page<Advpos> page1 = advposService.page(pageinfo, lqw);

//        long count = (long)advposService.count(lqw);
        BeanUtils.copyProperties(page1,advposDtoPage,"records");
        List<Advpos> records = pageinfo.getRecords();

        List<AdvposDto> list = records.stream().map((item) ->{
            AdvposDto advposDto = new AdvposDto();

            BeanUtils.copyProperties(item,advposDto);

            Integer advposId = item.getId();

            LambdaQueryWrapper<Advimg> wrapper = new LambdaQueryWrapper<>();
            wrapper.orderByAsc(Advimg::getAdvimgpos);
            wrapper.eq(Advimg::getAdvimgpos,advposId);
            int count = advimgService.count(wrapper);

            advposDto.setAdvposnum(count);

            return advposDto;

        }).collect(Collectors.toList());


//        for (Advpos advpos : data) {
//            AdvposDto advopDto = new AdvposDto();
//            BeanUtils.copyProperties(advpos, advopDto);
//            Integer id = advpos.getId();
//            LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Advimg::getAdvimgpos, id);
//            int count1 = advimgService.count(queryWrapper);
//            advopDto.setAdvposnum(count1);
//            data.add(advopDto);
//        }

        return ResultData.ok("", PageResult.init(page1.getTotal(),limit,page, list));

    }




    @GetMapping("all")
    public ResultData all(){
        List<Advpos> list = advposService.list();
        return ResultData.ok("",list);
    }



    //新增
    @PostMapping("/add")
    public ResultData save(@RequestBody Advpos advpos){

        advposService.save(advpos);

        return ResultData.ok("添加成功", null);

    }



    //回显
    @GetMapping("/rowInfo")
    public ResultData getById( int id){
        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advpos::getId, id);
        Advpos advpos = advposService.getById(id);

        return ResultData.ok("",advpos);

    }


    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody Advpos advpos){

        advposService.updateById(advpos);

        return ResultData.ok("修改成功",null);

    }


    //根据id删除单个
    @GetMapping("del")
    public ResultData delete(int id){

        advposService.removeById(id);

        return ResultData.ok("删除成功",null);

    }




}
